using UnityEngine;
using QFramework;

namespace QFramework.Monk
{
	
	public enum BigBossState
	{
		SlowMove,
		Idle,
		FastMove,
	}
	
	public partial class BigBoss : EnemyBase
	{
		
		private FSM<BigBossState> fsm = new FSM<BigBossState>();
		
		protected override void Awake()
		{
			base.Awake();
			SetFsmState();
		}

		private void SetFsmState()
		{
			//加入状态机
			fsm.State(BigBossState.SlowMove).OnUpdate(() =>
			{
				if (!AppStart.CheckGameState(GameState.GamePlaying))
				{
					return;
				}
				// 空指针检查
				if (Player.Default != null)
				{
					// 计算位置差
					var distance = Vector3.Distance(Player.Default.transform.position, this.transform.position);
					if (distance<6)
					{
						//进入下个状态
						fsm.ChangeState(BigBossState.Idle);
					}
					else
					{
						
						var dir = (Player.Default.transform.position - this.transform.position).normalized;

						// 移动物体
						selfRigidbody2D.velocity = dir * mSpeed;
					}
				}
				else
				{
					selfRigidbody2D.velocity = Vector2.zero;
				}
			});
			
			fsm.State(BigBossState.Idle).OnEnter(() =>
			{
				selfRigidbody2D.velocity = Vector2.zero;
			}).OnUpdate(()=>
			{
				//15-3 闪动颜色
				var frame = 3 + (60 * 3 - fsm.FrameCountOfCurrentState) / 10;
				if ((fsm.FrameCountOfCurrentState/frame)%2==0)
				{
					mSprite.color = Color.yellow;
				}
				else
				{
					mSprite.color = Color.white;
				}
				if (fsm.FrameCountOfCurrentState>=60*3)//发呆3秒
				{
					//进入下个状态
					fsm.ChangeState(BigBossState.FastMove);
				}
			}).OnExit(() =>
			{
				mSprite.color = Color.white;
			});

			
			fsm.State(BigBossState.FastMove).OnUpdate(() =>
			{
				if (!AppStart.CheckGameState(GameState.GamePlaying))
				{
					return;
				}
				// 空指针检查
				if (Player.Default != null)
				{

					if (fsm.FrameCountOfCurrentState < 60 * 2) //加速2秒
					{
						var dir = (Player.Default.transform.position - this.transform.position).normalized;

						// 加速移动
						selfRigidbody2D.velocity = dir * mSpeed*2;
					}
					else
					{
						//进入下个状态
						fsm.ChangeState(BigBossState.SlowMove);
					}
				}
				else
				{
					selfRigidbody2D.velocity = Vector2.zero;
				}
			});
			fsm.StartState(BigBossState.SlowMove);
		}

		protected override void FixedUpdate()
		{
			fsm.FixedUpdate();

		}
		protected override void Update()
		{
			fsm.Update();
		}
	}
}
